import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Read as T
import qualified Data.Text.IO as T

import qualified Data.ByteString as B

import Data.List
import qualified Data.List.Ordered as Ord

cmp :: T.Text ->  T.Text -> Ordering
cmp x y = let nx = T.takeWhile (/=':') x
              ny = T.takeWhile (/=':') y
          in compare nx ny

mergeWords :: T.Text ->  T.Text -> T.Text
mergeWords w1 w2 = let n1 = T.dropWhile (/=':') w2
                       n2 = T.tail n1
                       c = T.count n2 w1
                   in if c > 0 
                      then w1
                      else T.concat [w1, T.singleton ',', n2]
          
mergeDuplicates :: [T.Text] -> T.Text -> [T.Text]
mergeDuplicates [] _ = []
mergeDuplicates (x:xs) curWord | cmp curWord x == EQ = mergeDuplicates xs (mergeWords curWord x)
                               | otherwise = [curWord] ++ mergeDuplicates xs x
          
main :: IO ()
main = do
    dict1 <- B.readFile "dict.txt"
    dict2 <- B.readFile "dict2.txt"
    let dict = Ord.sortBy cmp  (T.lines ( T.append (T.decodeUtf8 dict1) ( T.decodeUtf8 dict2)))
        nubDict = mergeDuplicates (tail dict) (head dict)
    B.writeFile "glob2.txt" (T.encodeUtf8 (T.unlines nubDict))
               
